/**
 * 编辑道闸参数
 * @author xiaoxujs
 */

import { Form, Input, message, Modal } from "antd"
import { FC, useEffect } from "react"
import { IBaseBarrierConfigList, IBaseBarrierConfigUpdateFrom, IBaseBarrierConfigUpdateParams } from "../../../../../type/wulian";
import { postBaseBarrierConfigUpdate } from "../../../api";

const ChangeParams: FC<{
    open: boolean,
    data?: IBaseBarrierConfigList,
    ruChuFun: () => void
}> = ({
    open,
    data,
    ruChuFun
}) => {
        const [form] = Form.useForm<IBaseBarrierConfigUpdateFrom>();
        const { setFieldsValue, validateFields } = form;
        useEffect(() => {
            console.log(data)
            if (data) {

                setFieldsValue(data)
            }
            return () => { }
        }, [data, setFieldsValue])

        const handleOk = async () => {
            const values = await validateFields();
            if (data) {
                const params: IBaseBarrierConfigUpdateParams = {
                    ...data,
                    ...values,
                }
                const { code, msg } = await postBaseBarrierConfigUpdate(params) as { code: number, msg: string };
                if (code === 20000) {
                    message.success('设置成功');
                    ruChuFun();
                } else {
                    message.error(msg);
                }
            }

        };

        const handleCancel = () => {
            ruChuFun()
        };
        return <Modal title="编辑参数" open={open} onOk={handleOk} onCancel={handleCancel}>
            <Form
                labelCol={{ span: 7 }}
                wrapperCol={{ span: 16 }}
                form={form}
            >
                <Form.Item<IBaseBarrierConfigUpdateFrom>
                    label="IP地址"
                    name="barrierIp"
                    rules={[{ required: true, message: '请输入!' }]}
                >
                    <Input />
                </Form.Item>
                <Form.Item<IBaseBarrierConfigUpdateFrom>
                    label="端口号"
                    name="port"
                    rules={[{ required: true, message: '请输入!' }]}
                >
                    <Input />
                </Form.Item>
                <Form.Item<IBaseBarrierConfigUpdateFrom>
                    label="用户名"
                    name="barrierUser"
                    rules={[{ required: true, message: '请输入!' }]}
                >
                    <Input />
                </Form.Item>
                <Form.Item<IBaseBarrierConfigUpdateFrom>
                    label="密码"
                    name="barrierPassword"
                    rules={[{ required: true, message: '请输入!' }]}
                >
                    <Input />
                </Form.Item>
                <Form.Item<IBaseBarrierConfigUpdateFrom>
                    label="输出端口"
                    name="portNum"
                    rules={[{ required: true, message: '请输入!' }]}
                >
                    <Input />
                </Form.Item>
                <Form.Item<IBaseBarrierConfigUpdateFrom>
                    label="RTSP视频流地址"
                    name="streamAddr"
                    rules={[{ required: true, message: '请输入!' }]}
                >
                    <Input />
                </Form.Item>
                <Form.Item<IBaseBarrierConfigUpdateFrom>
                    label="设备序列号"
                    name="equipmentNum"
                    rules={[{ required: true, message: '请输入!' }]}
                >
                    <Input />
                </Form.Item>
                <Form.Item<IBaseBarrierConfigUpdateFrom>
                    label="备注"
                    name="remark"
                >
                    <Input />
                </Form.Item>

            </Form>
        </Modal>
    }

export default ChangeParams;